﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TestPlatform.Core;
using Aquiles.Helpers.Encoders;
using Apache.Cassandra;
using Aquiles.Helpers;
using Aquiles.Core.Cluster;
using Aquiles.Cassandra10;

namespace TestPlatform.Performer.Cassandra
{
    public class CassandraHelper : IDatabaseHelper
    {
        public const string UserCF = "Users";
        public const string PostCF = "Posts";

        public static void InsertUser(Guid id, string login, string name)
        {
            byte[] key = ByteEncoderHelper.GuidEncoder.ToByteArray(id);
            ColumnParent columnParent = new ColumnParent();

            Column loginColumn = new Column()
            {
                Name = ByteEncoderHelper.UTF8Encoder.ToByteArray("login"),
                Value = ByteEncoderHelper.UTF8Encoder.ToByteArray(login),
                Timestamp = UnixHelper.UnixTimestamp,

            };

            Column nameColumn = new Column()
            {
                Name = ByteEncoderHelper.UTF8Encoder.ToByteArray("name"),
                Value = ByteEncoderHelper.UTF8Encoder.ToByteArray(name),
                Timestamp = UnixHelper.UnixTimestamp,

            };

            columnParent.Column_family = UserCF;

            ICluster cluster = AquilesHelper.RetrieveCluster("MGR-Cluster");
            cluster.Execute(new ExecutionBlock(c =>
            {
                c.insert(key, columnParent, loginColumn, ConsistencyLevel.ONE);
                c.insert(key, columnParent, nameColumn, ConsistencyLevel.ONE);
                return null;
            }), "MGR");
        }

        public static void InsertPost(Guid id, string text, Guid userId)
        {
            byte[] key = ByteEncoderHelper.GuidEncoder.ToByteArray(id);
            ColumnParent columnParent = new ColumnParent();

            Column textColumn = new Column()
            {
                Name = ByteEncoderHelper.UTF8Encoder.ToByteArray("text"),
                Value = ByteEncoderHelper.UTF8Encoder.ToByteArray(text),
                Timestamp = UnixHelper.UnixTimestamp,

            };

            Column userIdColumn = new Column()
            {
                Name = ByteEncoderHelper.UTF8Encoder.ToByteArray("userId"),
                Value = ByteEncoderHelper.GuidEncoder.ToByteArray(userId),
                Timestamp = UnixHelper.UnixTimestamp,

            };

            columnParent.Column_family = PostCF;

            ICluster cluster = AquilesHelper.RetrieveCluster("MGR-Cluster");
            cluster.Execute(new ExecutionBlock(c =>
            {
                c.insert(key, columnParent, textColumn, ConsistencyLevel.ONE);
                c.insert(key, columnParent, userIdColumn, ConsistencyLevel.ONE);
                return null;
            }), "MGR");
        }


        public List<Guid> SeedDatabase(int count)
        {
            List<Guid> ids = new List<Guid>();

            for (int i = 0; i < count; i++)
            {
                Guid id = Guid.NewGuid();
                ids.Add(id);

                InsertUser(id, id.ToString(), id.ToString());
            }

            return ids;
        }

        public void ClearDatabase()
        {
            //ICluster cluster = AquilesHelper.RetrieveCluster("MGR-Cluster");
            //cluster.Execute(new ExecutionBlock(c =>
            //{
            //    c.truncate(UserCF);

            //    return null;
            //}), "MGR");
        }
    }
}
